package com.cv4j.app.fragment;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseFragment;
import com.cv4j.codeview.CodeBuilder;
import com.cv4j.codeview.CodeView;
import com.cv4j.rxcv4j.RxImageData;

/**
 * io读写
 *
 * @since 2021-05-14
 */
public class IOFragment extends BaseFragment {
    private Image image1;
    private Image image2;
    private CodeView codeview1;
    private CodeView codeview2;
    private RxImageData rxImageData;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component root = scatter.parse(ResourceTable.Layout_fragment_io, container, false);
        image1 = (Image) root.findComponentById(ResourceTable.Id_image1);
        image2 = (Image) root.findComponentById(ResourceTable.Id_image2);
        codeview1 = (CodeView) root.findComponentById(ResourceTable.Id_codeview1);
        codeview2 = (CodeView) root.findComponentById(ResourceTable.Id_codeview2);
        initDate();
        return root;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }

    private void initDate() {
        image1.setPixelMap(ResourceTable.Media_test_io);
        rxImageData = RxImageData.bitmap(image1.getPixelMap());
        rxImageData.into(image2);
        CodeBuilder codeBuilder = new CodeBuilder();
        codeBuilder.append("CV4JImage cv4JImage = new CV4JImage(bitmap);")
            .lineFeed()
            .append("image2.setImageBitmap(cv4JImage.toBitmap());");
        codeview1.showCode(codeBuilder.build());
        codeBuilder = new CodeBuilder();
        codeBuilder.appendHljsKeyword("RxImageData")
            .appendHljsName(".bitmap")
            .append("(")
            .appendHljsKeyword("bitmap")
            .append(")")
            .appendHljsName(".into")
            .append("(")
            .appendHljsKeyword("image2")
            .append(");");
        codeview2.showCode(codeBuilder.build());
    }

    @Override
    protected void onStop() {
        super.onStop();
        rxImageData.recycle();
    }
}
